﻿using System;
using System.Collections.ObjectModel;
using System.IO;
using BeeCoders.Tools.Wpf.Infrastructure;

namespace BeeCoders.Tools.Wpf.FileExplorer
{
	public class FileExplorerViewModel : ViewModelBase
	{
		#region Property: IsMultiSelect
		private bool isMultiSelect;
		public bool IsMultiSelect
		{
			get
			{
				return isMultiSelect;
			}
			set
			{
				isMultiSelect = value;
				RaisePropertyChanged("IsMultiSelect");

				UpdateView();
			}
		}
		#endregion
		
		#region Property: ShowDirectories
		private bool showDirectories;
		public bool ShowDirectories
		{
			get
			{
				return showDirectories;
			}
			set
			{
				showDirectories = value;
				RaisePropertyChanged("ShowDirectories");
			}
		}
		#endregion
		
		#region Property: ShowFiles
		private bool showFiles;
		public bool ShowFiles
		{
			get
			{
				return showFiles;
			}
			set
			{
				showFiles = value;
				RaisePropertyChanged("ShowFiles");
			}
		}
		#endregion
		
		#region Property: ShowIcons
		private bool showIcons;
		public bool ShowIcons
		{
			get
			{
				return showIcons;
			}
			set
			{
				showIcons = value;
				RaisePropertyChanged("ShowIcons");

				UpdateView();

			}
		}
		#endregion
		
		#region Property: ShowRealIcons
		private bool showRealIcons;
		public bool ShowRealIcons
		{
			get
			{
				return showRealIcons;
			}
			set
			{
				showRealIcons = value;
				RaisePropertyChanged("ShowRealIcons");
			}
		}
		#endregion


		#region Property: IsFtpExplorer
		private bool isFtpExplorer;
		public bool IsFtpExplorer
		{
			get
			{
				return isFtpExplorer;
			}
			set
			{
				isFtpExplorer = value;
				RaisePropertyChanged("IsFtpExplorer");
			}
		}
		#endregion
		

		#region Property: SelectedItem
		private string selectedItem;
		public string SelectedItem
		{
			get
			{
				return selectedItem;
			}
			set
			{
				selectedItem = value;
				RaisePropertyChanged("SelectedItem");
			}
		}
		#endregion
		
		#region Property: SelectedItems
		private ObservableCollection<string> selectedItems;
		public ObservableCollection<string> SelectedItems
		{
			get
			{
				return selectedItems;
			}
			set
			{
				selectedItems = value;
				RaisePropertyChanged("SelectedItems");
			}
		}
		#endregion

		private static FileExplorerViewModel instance;
		public static FileExplorerViewModel Instance
		{
			get
			{
				if (instance == null)
				{
					instance = new FileExplorerViewModel();
				}
				return instance;
			}
		}

		public ObservableCollectionEx<DriveViewModel> Drives
		{
			get;
			set;
		}

		public void Initialize()
		{
			SelectedItems = new ObservableCollection<string>();

			if (!isFtpExplorer)
			{
				InitFileSystem();
			}
			else
			{
				InitFtpSystem();
			}
		}

		private void InitFileSystem()
		{
			Drives = new ObservableCollectionEx<DriveViewModel>();

			foreach (DriveInfo driveInfo in System.IO.DriveInfo.GetDrives())
			{
				Console.WriteLine("{0} {1} {2}", driveInfo.Name, driveInfo.IsReady, driveInfo.DriveType);

				if (driveInfo.IsReady)
					Drives.Add(new DriveViewModel(driveInfo.Name, driveInfo.RootDirectory.Name, driveInfo, ShowDirectories, ShowFiles));
				else
					Drives.Add(new DriveViewModel(driveInfo.Name, driveInfo.Name, driveInfo, ShowDirectories, ShowFiles));

			}
		}

		private void InitFtpSystem()
		{
			Drives = new ObservableCollectionEx<DriveViewModel>();

			Drives.Add(new DriveViewModel("ftp.livedrive.com", "", ShowDirectories, ShowFiles));

		}

		private void UpdateView()
		{
			if (Drives == null) return;

			foreach (var driveViewModel in Drives)
			{
				driveViewModel.RaisePropertyChanged("HasCheckbox");
				driveViewModel.RaisePropertyChanged("ShowIcons");
				driveViewModel.UpdateView();
			}

		}
		public FileExplorerViewModel()
		{
		}
	}

	public class FileExplorerDemo : FileExplorerViewModel
	{
		public FileExplorerDemo()
		{
			SelectedItems = new ObservableCollection<string>();
			Drives = new ObservableCollectionEx<DriveViewModel>();
			foreach (DriveInfo driveInfo in System.IO.DriveInfo.GetDrives())
			{
				Drives.Add(new DriveViewModel(driveInfo.Name, driveInfo.RootDirectory.Name, driveInfo,true,true));
			}

		}
	}

}
